package com.echo.code.spring.spl;

import com.echo.code.spring.core.Car;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * Description
 *
 * @author baiyp
 * @version 1.0.0
 * @date 2019/04/14
 */
public class SplClient {

  public static void main(String[] args) {
    SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, SplClient.class.getClassLoader());
    ExpressionParser parse = new SpelExpressionParser(configuration);
    // one
    Expression expression1 = parse.parseExpression("'one ' + 'two'");
    String msg = expression1.getValue(String.class);
    System.out.println(msg);
    // two
    Expression expression2 = parse.parseExpression("'HelloWorld'.concat('!')");
    String str = expression2.getValue(String.class);
    System.out.println(str);
    // three
    Car car = new Car();
    car.setName("Audi");
    car.setPrice(2000.00);
    EvaluationContext context = new StandardEvaluationContext(car);
    Expression expression3 = parse.parseExpression("price");
    double price = expression3.getValue(context, double.class);
    System.out.println(price);

  }
}
